package com.luyinbros.imagepicker;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.luyinbros.imagepicker.model.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Created by Andy Hong on 6/30/2017.
 */

public class ImageDataHelper {
    private List<LocalMedia> list;
    private List<LocalMedia> selectList;

    private int maxSelectNum;

    public ImageDataHelper(int maxSelectNum) {
        if (maxSelectNum > 0) {
            this.selectList = new ArrayList<>(maxSelectNum);
        }
        this.maxSelectNum = maxSelectNum;
    }

    public int getMaxSelectNum() {
        return maxSelectNum;
    }

    public ImageDataHelper(List<LocalMedia> selectList, int maxSelectNum) {
        if (selectList == null) {
            this.selectList = new ArrayList<>(maxSelectNum);
        } else {
            this.selectList = selectList;
        }
        this.maxSelectNum = maxSelectNum;
    }

    public boolean isMaxSelect() {
        return selectList.size() == maxSelectNum;
    }

    public void setList(@Nullable List<LocalMedia> list) {
        this.list = list;
    }

    public void setSelectList(List<LocalMedia> selectList) {
        this.selectList = selectList;
    }

    public LocalMedia getItem(int position) {
        return list.get(position);
    }


    public int allSize() {
        return list == null ? 0 : list.size();
    }

    public int selectedSize() {
        return selectList.size();
    }

    public List<LocalMedia> getSelectList() {
        return selectList;
    }

    public boolean isSelected(@NonNull LocalMedia localMedia) {
        for (int i = 0, size = selectList.size(); i < size; i++) {
            if (localMedia.compareTo(selectList.get(i)) == 0) {
                return true;
            }
        }
        return false;
    }

    public List<LocalMedia> getAllList() {
        return list;
    }

    public void select(@NonNull LocalMedia localMedia) {
        int selectPosition = -1;
        for (int i = 0; i < selectList.size(); i++) {
            if (localMedia.compareTo(selectList.get(i)) == 0) {
                selectPosition = i;
                break;
            }
        }
        if (selectPosition == -1) {
            selectList.add(localMedia);
        } else {
            selectList.set(selectPosition, localMedia);
        }

    }

    public void removeSelect(@NonNull LocalMedia localMedia) {
        for (LocalMedia media : selectList) {
            if (media.compareTo(localMedia) == 0) {
                selectList.remove(media);
                break;
            }
        }
    }


}
